home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / tex / nocz12.zip / NOCZ2.ASM < prev    next >
Assembly Source File  |  1989-10-13  |  6KB  |  203 lines

  1.     Name nocz
  2.     Title    Control Z stripping filter.
  3.     page    ,132
  4. Comment ~
  5.  
  6.     This program is a filter that removes all occurrances
  7.     of the ASCII character 26 (^Z) from its output stream.
  8.     It will optionally accept a filename from which to read
  9.     input.  All output goes to the     standard output device
  10.     unless redirected.
  11.  
  12.     Examples:
  13.         nocz abc.txt    read input from abc.txt,
  14.                 output goes to screen.
  15.         nocz abc.txt >abcnew.txt
  16.                 read input from abc.txt,
  17.                 output goes to abcnew.txt.
  18.         nocz <abc.txt >abcnew.txt
  19.                 same as previous example
  20. Toad Hall Tweak, Oct 89
  21.  - Bumping working buffer to BIG...
  22.  - Simplistic PSP command line parsing didn't properly handle
  23.    DOS redirection/filter usage.  Fixed.
  24.  - No reason at all to release memory .. unless maybe for filters??
  25.  - v1.2 replaces Ctrl Z's with an innocuous space.
  26.    Cleaner code but not significantly faster than v1.1's lodsb/test/stosb
  27.    loop!
  28.    If you don't wish this function and wish the Ctrl Z's truly deleted,
  29.    use v1.1.
  30.  
  31. ~
  32.  
  33. CR    equ    0DH
  34. LF    equ    0AH
  35. STDOUT    equ    1        ;Standard Output (normally console)    v1.2
  36.  
  37. ;===================================================================
  38.     CSEG    segment    public para 'CODE'
  39. ;===================================================================
  40. ;
  41. ;    command line is at 80h of psp - first byte is length
  42. ;
  43.     ORG    80h
  44.  
  45. cmdline    label    byte            ;                v1.1
  46.  
  47.     ORG    100h            ; com file starts here
  48.     ASSUME    CS:CSEG,DS:CSEG,ES:CSEG
  49.  
  50. NoCz    proc    near            ;v1.1
  51.  
  52.     mov    AH,30h            ; get dos version
  53.     int    21h
  54.     cmp    AL,2            ; must be at least 2.0
  55.     jb    Oops
  56.  
  57.     call    Parse_CmdLine        ;parse cmdline for target filename v1.1
  58.                     ;(will give help and die if NO
  59.                     ;filename or redirection)
  60.  
  61.     xor    bp,bp            ;assume StdIn handle        v1.1
  62.     or    dx,dx            ;did we get a name?        v1.1
  63.     jz    Save_Handle        ;nope, use StdIn        v1.1
  64.  
  65. ;v1.1    else DX -> filename start
  66.  
  67. ; now try to open the file
  68.  
  69.     mov    ax,3D00H        ;open file, read only        v1.1
  70.     int    21h            ; invoke function
  71.     jc    Oops            ; open error - quit
  72.  
  73.     mov    bp,ax            ;keep file handle in BP        v1.1
  74.  
  75. Save_Handle:                ;                v1.1
  76.     mov    DX,offset buffer    ;DX -> read/write buffer    v1.1
  77.  
  78. Again:
  79.     mov    BX,bp    ;handle        ; read from standard input or file v1.1
  80.     mov    CX,BUFSIZ        ; # of characters to read    v1.1
  81.     mov    AH,3Fh            ; dos read function
  82.     int    21h            ; invoke function
  83.     jc    Oops            ; error!  (error value in AL)    v1.1
  84.     or    ax,ax            ; if zero, end of file        v1.1
  85.     jz    Oops            ; (errorlevel 0)        v1.1
  86.  
  87. ;v1.2    Instead of the lodsb/test/stosb loop in v1.1,
  88. ;    we'll use the more elegant scasb function to seek out
  89. ;    those Ctrl Z's.
  90.  
  91.     mov    si,ax            ;save nr chars read        v1.2
  92.     mov    CX,AX            ; CX contains # characters read
  93.     mov    ax,201AH        ; AL = Ctrl Z to scan for    v1.2
  94.                     ; AH = space replacement    v1.2
  95.     mov    di,dx    ;offset buffer    ; ES:DI is buffer to scan    v1.2
  96.     mov    bx,-1            ; to point (DI:BX) to prev char    v1.2
  97.  
  98. ScanLup:
  99.     repne    scasb            ;find first Ctrl Z        v1.2
  100.     jnz    Skip            ;none found, done        v1.2
  101.      mov    [di][bx],ah        ;replace with a space        v1.2
  102.      or    cx,cx            ;all done?            v1.2
  103.      jnz    ScanLup            ;nope, back for more        v1.2
  104.  
  105. Skip:
  106.     mov    cx,si            ; orig nr chars read        v1.2
  107.     mov    bx,STDOUT        ; standard output device    v1.2
  108.     mov    AH,40h            ; dos write function
  109.     int    21h            ; invoke dos function
  110.  
  111.     jnc    Again            ; if no write error ..        v1.1
  112.                     ; repeat until EOF or read error v1.1
  113.  
  114. Oops:
  115.     mov    ah,4CH            ;terminate (errorlevel in AL)    v1.1
  116.     int    21H            ;                v1.1
  117.  
  118. NoCz    endp                ;                v1.1
  119.  
  120.     even                ;                v1.1
  121.  
  122. buffer    LABEL    NEAR
  123.  
  124. BUFSIZ    equ    NOT ($ - CSEG + 256)    ;use full segment        v1.1
  125.                     ;minus 256-byte stack
  126.                     ;and program size
  127.  
  128. ;v1.1    Parse commandline for target filename
  129. ;    Handle redirection spaces
  130.  
  131. Parse_CmdLine    proc    near
  132.  
  133.     mov    si,offset cmdline    ;cmd parm length byte
  134.     lodsb                ;snarf length byte
  135.                     ;(SI -> 1st char)
  136.     xor    ah,ah            ;clear msb
  137.     mov    cx,ax            ;cmdline length is loop counter
  138.     jcxz    Help            ;no cmdline, give help and die
  139.  
  140. ;v1.1    When redirection happens, DOS leaves spaces instead of simply
  141. ;    a null line!
  142. ;    Gotta allow for that by gobbling leading spaces.
  143. ;    If spaces is all we had .. we have redirection!
  144.  
  145.     xor    dx,dx            ;clear DX (don't have a name)
  146.     mov    ah,20H            ;handy constant
  147.  
  148. ParseLup:
  149.     lodsb                ;snarf cmdline char
  150.     cmp    al,ah            ;space?
  151.     jz    Relup            ;yep, skip it
  152.      cmp    al,CR            ;terminating CR? (no name)
  153.      jnz    Got_Name        ;nope, must be real name
  154. Relup:
  155.     loop    ParseLup        ;do all cmdline chars
  156.  
  157. ;If we fall thru .. there were only spaces (redirection).
  158.  
  159.     ret                ;with DX = 0 (no name)
  160.  
  161. ;SI -> target filename's first char (non-space)
  162.  
  163. Got_Name:
  164.     mov    dx,si            ;DX -> filename's first char +1
  165.     dec    dx            ;back up to real first char
  166.     mov    ax,CR            ;scan for terminating CR
  167.                     ;AH=0
  168. Name_Lup:
  169.     cmp    [si],al            ;terminating CR?
  170.     jz    AsciiZ_Name        ;yep, stuff a 0 there
  171.      inc    si            ;bump filename ptr
  172.      loop    Name_Lup        ;nope, keep going
  173.  
  174. ;If we fell through .. something is SEVERELY wrong with our
  175. ;command line count!  Shouldn't ever happen.
  176.  
  177. AsciiZ_Name:
  178.     mov    [si],ah            ;AsciiZ the name
  179.     ret
  180.     
  181. Parse_CmdLine    endp
  182.  
  183.  
  184. ;v1.1    Provide help if no cmd line parms
  185.  
  186. Help    proc    near            ;                v1.1
  187.     mov    dx,offset help$        ;help msg
  188.     mov    ah,9            ;display msg
  189.     int    21H
  190.     mov    ax,4C00H        ;terminate (errorlevel 0)    v1.1
  191.     int    21H            ;                v1.1
  192.  
  193. help$    db    'NoCZ v1.2 - strips Ctrl Z characters from a file.',CR,LF
  194.     db    'Usage:  nocz [<] filename.typ [>output]',CR,LF
  195.     db    'Input may be a proper path:\filename or redirection.',CR,LF
  196.     db    'Output may be redirected as desired (default CON:).'
  197.     db    CR,LF,'$'
  198.  
  199. Help    endp
  200.  
  201. CSEG    ENDS
  202.     END    NoCz
  203.